package com.xn.app.muying.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xn.app.muying.MuYingApplication;
import com.xn.app.muying.R;
import com.xn.app.muying.adpter.ChanJianListAdapter;
import com.xn.app.muying.adpter.MineYiYuanAdapter;
import com.xn.app.muying.bean.ChanJianListBean;
import com.xn.app.muying.bean.MineYiYuanBean;
import com.xn.app.muying.util.APIMethod;
import com.xn.app.muying.util.Constants;
import com.xn.app.muying.util.HttpUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Request;

/**
 * Created by Administrator on 2016/10/28.
 */
public class ChanJianListActivity extends AppCompatActivity implements View.OnClickListener, HttpUtil.OnGetDataListener {

    private GridView gridView;
    private TextView tv_hospitalname;
    private MineYiYuanAdapter adapter;
    private Handler handler = new Handler();
    private ArrayList<MineYiYuanBean> list;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chanjian_list);
        Intent intent = getIntent();
        String hospitalId = intent.getStringExtra("hospitalId");

        initView();

        loadMessage(hospitalId);

        HttpUtil.getData(APIMethod.Mine_JiuZhenYiYuan, handler, this);
        adapter = new MineYiYuanAdapter(this);

    }

    private void initView() {
        gridView= (GridView) findViewById(R.id.activity_chanjian_gridview);
        tv_hospitalname= (TextView) findViewById(R.id.activity_chuanjian_hospitalname);
        tv_hospitalname.setOnClickListener(this);
    }

    private void loadMessage(final String hospitalId) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    String tokens = Constants.getStringByName(ChanJianListActivity.this, "yonghu");
                    Type type1 = new TypeToken<HashMap<String, String>>() {
                    }.getType();
                    HashMap<String, String> token = new Gson().fromJson(tokens, type1);
                    Request request = new Request.Builder().url("http://apitest.beierbaby.com/api/antenatalcares/user/antenatalcare/list?hospitalId="+hospitalId).addHeader("Authorization", "Bearer " + token.get("access_token")).get().build();
                    okhttp3.Response response = MuYingApplication.mOkHttpClient.newCall(request).execute();

                    String bodys = response.body().string();
                    onJsonMessage(bodys);
                    Log.i("bodysmessage==",bodys);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void onJsonMessage(String bodys) {
        try {
            JSONObject jsonObject = new JSONObject(bodys);
            String hospitalId = jsonObject.optString("hospitalId");
            final String hospitalName = jsonObject.optString("hospitalName");
            JSONArray antenatalCares = jsonObject.getJSONArray("antenatalCares");
            final ArrayList<ChanJianListBean> list = new ArrayList<>();
            for (int i = 0; i < antenatalCares.length(); i++) {
                JSONObject jsonObject1 = antenatalCares.getJSONObject(i);
                String id = jsonObject1.optString("id");
                String title = jsonObject1.optString("title");
                String order = jsonObject1.optString("order");
                String startWeek = jsonObject1.optString("startWeek");
                String endWeek = jsonObject1.optString("endWeek");
                String status = jsonObject1.optString("status");
                ChanJianListBean chanJianListBean = new ChanJianListBean();
                chanJianListBean.setId(id);
                chanJianListBean.setTitle(title);
                chanJianListBean.setOrder(order);
                chanJianListBean.setStartWeek(startWeek);
                chanJianListBean.setEndWeek(endWeek);
                chanJianListBean.setStatus(status);
                list.add(chanJianListBean);
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tv_hospitalname.setText(hospitalName);
                    ChanJianListAdapter chanJianListAdapter=new ChanJianListAdapter(ChanJianListActivity.this,list);
                    gridView.setAdapter(chanJianListAdapter);
                }
            });

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.activity_chuanjian_hospitalname:
                AlertDialog.Builder radioDialog = new AlertDialog.Builder(this);
                radioDialog.setTitle("医院");
                radioDialog.setIcon(R.drawable.ic_launcher);
                radioDialog.setSingleChoiceItems(adapter, 0,
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                tv_hospitalname.setText(list.get(which).getName().toString());
                            }
                        });
                radioDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                AlertDialog dialogRadio = radioDialog.create();
                dialogRadio.show();
                break;
        }
    }

    @Override
    public void onJsonData(String data) {
        try {
            JSONObject jsonObject = new JSONObject(data);
            JSONArray items = jsonObject.getJSONArray("items");
            list = new ArrayList<>();
            for (int i = 0; i < items.length(); i++) {
                JSONObject object = items.getJSONObject(i);
                String name = object.optString("name");
                int id = object.optInt("id");
                MineYiYuanBean bean = new MineYiYuanBean();
                bean.setName(name);
                bean.setId(id);
                list.add(bean);

            }
            adapter.getData(list);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
